package cn.bs.busuo.account.service.impl;

import cn.bs.busuo.account.dao.persist.repository.ISightRepository;
import cn.bs.busuo.account.dao.persist.repository.ISightsSeasonsRepository;
import cn.bs.busuo.account.pojo.entity.SightsSeasons;
import cn.bs.busuo.account.pojo.param.SightSeason;
import cn.bs.busuo.account.pojo.vo.SeasonVO;
import cn.bs.busuo.account.pojo.vo.SightVO;
import cn.bs.busuo.account.pojo.vo.SightsSeasonsVO;
import cn.bs.busuo.account.service.ISightsSeasonsService;
import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class SightsSeasonsServiceImpl implements ISightsSeasonsService {
    @Autowired
    private ISightsSeasonsRepository sightsSeasonsRepository;
    @Autowired
    private ISightRepository sightRepository;

    /**
     * 新增季节景点
     */

    @Override
    public void addSeason(SightSeason sightSeason) {
        SightsSeasons sightsSeasons=new SightsSeasons();
        BeanUtils.copyProperties(sightSeason,sightsSeasons);
//        判断传入的季节是否正确
        SeasonVO seasonVO = sightsSeasonsRepository.selectById(sightsSeasons.getBestSeasonId());
        if (seasonVO==null){
            String message = "添加失败，目前为设置该季节！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
//        todo 判断是否有该景点
        SightVO sightVO = sightRepository.selectById(sightsSeasons.getSightsId());
        if (sightVO==null){
            String message = "添加失败，该景点不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
//        判断是否为重复操作
        SightsSeasonsVO sightsSeasonsVO = sightsSeasonsRepository.selectByType(sightsSeasons);
        if (sightsSeasonsVO!=null){
            String message = "重复操作，已将该景点添加为此季节推荐！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        sightsSeasonsRepository.addSeason(sightsSeasons);
    }

    /**
     * 删除季节景点
     */
    @Override
    public void delectSeason(Long id) {
        sightsSeasonsRepository.delectSeason(id);
    }
}
